//
//  DropMenuView.m
//  TengyunStation
//
//  Created by Johnny on 15/8/31.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "DropMenuView.h"
#import "Singleton.h"
#import "TSHeader.h"

@interface MenuButton : UIButton {
    UILabel *_titleLabel;
}

@property(nonatomic,assign)NSInteger selectedNumber;

- (void)setTitle:(NSString *)title;

@end

@implementation MenuButton

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.selectedNumber = 0;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = MainTextColor;
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return self;
}

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state {
    _titleLabel.textColor = color;
}

- (void)setTitle:(NSString *)title {
    _titleLabel.text = title;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        [self setTitleColor:OrangeColor forState:UIControlStateNormal];
    } else {
        [self setTitleColor:MainTextColor forState:UIControlStateNormal];
    }
}

@end

@implementation DropMenuView

- (void)awakeFromNib {
    if (!_firstButton) {
        _firstButton = [[MenuButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width/2, self.frame.size.height)];
        [_firstButton setTitle:@"全部"];
        [_firstButton setBackgroundImage:[UIImage imageNamed:@"chax_tab_leftbg"] forState:UIControlStateNormal];
        [_firstButton setBackgroundImage:[UIImage imageNamed:@"chax_tab_leftbg_pre"] forState:UIControlStateSelected];
        [_firstButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_firstButton];
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(159, 0, 1, 44)];
        line.backgroundColor = [UIColor darkGrayColor];
    }
    if (!_secondButton) {
        _secondButton = [[MenuButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2, 0, self.frame.size.width/2, self.frame.size.height)];
        [_secondButton setTitle:@"全部状态"];
        [_secondButton setBackgroundImage:[UIImage imageNamed:@"chax_tab_rightbg"] forState:UIControlStateNormal];
        [_secondButton setBackgroundImage:[UIImage imageNamed:@"chax_tab_rightbg_pre"] forState:UIControlStateSelected];
        [_secondButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_secondButton];
    }
    if (!_bgView) {
        float y = 64 + 44.5 + self.frame.size.height;
        float height = [UIScreen mainScreen].bounds.size.height - y;
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 83, 320, height)];
        _bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBgView:)];
        tap.numberOfTapsRequired = 1;
        tap.delegate = self;
        [_bgView addGestureRecognizer:tap];
    }
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 0.1)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_bgView addSubview:_tableView];
    }
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled {
    [super setUserInteractionEnabled:userInteractionEnabled];
    if (userInteractionEnabled) {
        [_firstButton setTitleColor:MainTextColor forState:UIControlStateNormal];
        [_secondButton setTitleColor:MainTextColor forState:UIControlStateNormal];
    } else {
        [self setDefault];
        [_firstButton setTitleColor:TipTextColor forState:UIControlStateNormal];
        [_secondButton setTitleColor:TipTextColor forState:UIControlStateNormal];
    }
}

- (void)setDefault {
    [_firstButton setTitle:@"全部"];
    [_secondButton setTitle:@"全部状态"];
}

- (void)hideBgView:(UIGestureRecognizer *)recognizer {
    [self reloadBgView];
}

- (void)buttonAction:(UIButton *)button {
    if (button == _firstButton) {
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:1];
        ExpressCompany *express = [[ExpressCompany alloc] init];
        express.expressCompanyId = @"";
        express.expressCompanyName = @"全部";
        [mutableArray addObject:express];
        [mutableArray addObjectsFromArray:[Singleton shareInstance].expressCompanyList];
        _menuList = mutableArray;
        if (_firstButton.selected) {//说明展示当前按钮数据
            [self reloadBgView];
        } else {
            _firstButton.selected = YES;
            _secondButton.selected = NO;
            if (_isShow) {
                [self reloadTableView];
            } else {
                [self reloadBgView];
            }
        }
    } else if (button == _secondButton) {
        _menuList = @[@"全部状态", @"待自提", @"待配送", @"已逾期", @"已退件", @"客户签收", @"小站代签", @"客户自签"];
        if (_secondButton.selected) {//说明展示当前按钮数据
            [self reloadBgView];
        } else {
            _secondButton.selected = YES;
            _firstButton.selected = NO;
            if (_isShow) {
                [self reloadTableView];
            } else {
                [self reloadBgView];
            }
        }
    }
}

- (void)reloadTableView {
    [_tableView reloadData];
    [UIView animateWithDuration:0.2 animations:^{
        float height = 0;
        if (_menuList.count * 38 < _bgView.frame.size.height) {
            height = _menuList.count * 38;
        } else {
            height = _bgView.frame.size.height;
        }
        _tableView.frame = CGRectMake(self.frame.origin.x, 0, self.frame.size.width, height);
    }];
}

- (void)reloadBgView {
    if (_isShow) {
        _isShow = NO;
        _firstButton.selected = NO;
        _secondButton.selected = NO;
        [UIView animateWithDuration:0.2 animations:^{
            _tableView.frame = CGRectMake(0, 0, 320, 0);
        } completion:^(BOOL finished) {
            [_bgView removeFromSuperview];
        }];
    } else {
        _isShow = YES;
        [self.superview addSubview:_bgView];
        [self reloadTableView];
    }
}

#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint location = [gestureRecognizer locationInView:_bgView];
    if (location.y <= _tableView.frame.size.height) {
        return NO;
    }
    return YES;
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _menuList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 38.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"DropMenuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, cell.contentView.frame.size.height)];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.tag = 100;
        nameLabel.textColor = MainTextColor;
        [cell.contentView addSubview:nameLabel];
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 37.5, 320, 0.5)];
        line.backgroundColor = LineColor;
        [cell.contentView addSubview:line];
        cell.backgroundColor = UIColorFromRGB(0xfbfbfb);
    }
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
    if (_firstButton.selected) {
        ExpressCompany *company = [_menuList objectAtIndex:indexPath.row];
        nameLabel.text = company.expressCompanyName;
        if (_firstButton.selectedNumber == indexPath.row) {
            nameLabel.textColor = OrangeColor;
        } else {
            nameLabel.textColor = MainTextColor;
        }
    } else if (_secondButton.selected) {
        NSString *title = [_menuList objectAtIndex:indexPath.row];
        nameLabel.text = title;
        if (_secondButton.selectedNumber == indexPath.row) {
            nameLabel.textColor = OrangeColor;
        } else {
            nameLabel.textColor = MainTextColor;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_firstButton.selected) {
        _firstButton.selected = NO;
        ExpressCompany *company = [_menuList objectAtIndex:indexPath.row];
        [_firstButton setTitle:company.expressCompanyName];
        _firstButton.selectedNumber = indexPath.row;
        [self reloadBgView];
        if ([_delegate respondsToSelector:@selector(didFinishExpressCompanyId:)]) {
            [_delegate didFinishExpressCompanyId:company.expressCompanyId];
        }
    } else if (_secondButton.selected) {
        NSString *title = [_menuList objectAtIndex:indexPath.row];
        _secondButton.selected = NO;
        [_secondButton setTitle:title];
        _secondButton.selectedNumber = indexPath.row;
        [self reloadBgView];
        if ([_delegate respondsToSelector:@selector(didFinishExpressType:)]) {
            NSString *type = [NSString stringWithFormat:@"0%ld",(long)indexPath.row];
            [_delegate didFinishExpressType:type];
        }
    }
}

- (void)dealloc {
    _menuList = nil;
}

@end
